/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hoja7;

/**
 *
 * @author JJ
 */
public class Heap {
    private int tope;
    private Node[] nodos;
    private int amount;

    public Heap(int tope) {
        this.tope = tope;
        nodos =  new Node[tope];
        amount = 0;
    }

    public void insert(Node n) {
        int j;

        if(amount == 0)
            nodos[amount++] = n;
        else {
            for(j=amount -1; j>=0; j--) {
                if(n.getRepet() >= nodos[j].getRepet())
                    nodos[j+1] = nodos[j];
                else
                    break;

            }

            nodos[j+1] = n;
            amount= amount+1;
        }


    }

    public Node remove() {
        amount =amount-1;
        return nodos[amount];
    }


    public Node remove(Node nodo) {
        Node temp;
        for(int i =0; i < amount; i++) {
            if(nodos[i].getDato().toString().charAt(0) == nodo.getDato().toString().charAt(0)) {
                temp = nodos[i];
                nodos[i] = null;
                for(int j = i+1; j < amount; j++)
                    nodos[i] = nodos[j];
                amount =amount -1;
                return temp;
            } 
               

        }
  
        return null;
    }



    public Node seeFirst() {

        return nodos[amount-1];

    }

  

    public int getAmount() {
        return amount;
    }

    public Node getNode(Node nodo) {
        for(int i =0; i < amount; i++) {
            if(nodos[i].getDato().toString().charAt(0) == nodo.getDato().toString().charAt(0)) {
                return nodos[i];
            }
        }
        return null;
    }
    
}
